<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	Zend_Session::start();
    	
        $pretty_protected = new Zend_Session_Namespace('pretty_protected');
        if ($pretty_protected->accept_answer === true) {
            $this->view->unlock = 1;
        } else {
            $this->view->unlock = 0;
        }
    }

    public function indexAction()
    {
        $country = $this->getRequest()->getParam('c', 'mc');
        $userModel = new Application_Model_UserDetail();
        $listNewMember = $userModel->getNewMember($country);
        $listGoingViral = $userModel->getGoingViral($country);
        $listTopMember = $userModel->getTopMember($country);

        $this->view->listNewMember = $listNewMember;
        $this->view->listGoingViral = $listGoingViral;
        $this->view->listTopMember = $listTopMember;
        $this->view->countryCode = $country;
    }
    
    public function loadGoingViralAction()
    {
    	
    	$page = $this->sessionPage("page_viral");
        $country = $this->getRequest()->getParam('c', 'mc');
        $loadmoreUser = $this->getRequest()->getParam('end', '');
        $userModel = new Application_Model_UserDetail();
        $listGoingViral = $userModel->getGoingViral($country, $loadmoreUser, $page);
        $this->view->listGoingViral = $listGoingViral;
        
        $this->_helper->layout->disableLayout();
		$this->render('list-going-viral');
    }
    
    public function loadTopMemberAction()
    {
    	$page = $this->sessionPage("page_member");
        $country = $this->getRequest()->getParam('c', 'mc');
        $loadmoreUser = $this->getRequest()->getParam('end', '');
        $userModel = new Application_Model_UserDetail();
        $listTopMember = $userModel->getTopMember($country, $loadmoreUser, $page);
        $this->view->listTopMember = $listTopMember;
        
        $this->_helper->layout->disableLayout();
		$this->render('list-top-member');
    }
    
    private function sessionPage($session_name){
    	$page = 1;
    	if( Zend_Session::namespaceIsset($session_name) ){
    		$pageNs = new Zend_Session_Namespace($session_name);
    		$pageInfo = $pageNs->getIterator();
    		$page = $pageInfo["page"];
    		$page ++;
    		$pageNs->page = $page;
    	}else{
    		$pageNs = new Zend_Session_Namespace($session_name);
    		$page = 1;
    		$pageNs->page = $page;
    	}
    	return $page;
    }
    
}

